﻿// See https://aka.ms/new-console-template for more information
using System.Net;
using System.Net.Sockets;
using System.Text;
using TcpServer;
int sendport = 2002, reveiveport = 2001;
string name = "TcpServer";
Console.WriteLine(name);
List<IntPtr> intPtrs = new List<IntPtr>();
HPSocket.Tcp.TcpServer tcpServer = new HPSocket.Tcp.TcpServer();
tcpServer.Port = 8899;
tcpServer.OnReceive += TcpServer_OnReceive;
tcpServer.OnClose += TcpServer_OnClose;
tcpServer.OnAccept += TcpServer_OnAccept;

System.Net.Sockets.UdpClient sendUDP;
System.Net.Sockets.UdpClient receiveUDP;

sendUDP = new System.Net.Sockets.UdpClient();
receiveUDP = new System.Net.Sockets.UdpClient(reveiveport);

HPSocket.HandleResult TcpServer_OnAccept(HPSocket.IServer sender, IntPtr connId, IntPtr client)
{
    intPtrs.Add(connId);
    return HPSocket.HandleResult.Ok;
}

HPSocket.HandleResult TcpServer_OnClose(HPSocket.IServer sender, IntPtr connId, HPSocket.SocketOperation socketOperation, int errorCode)
{
    intPtrs.Remove(connId);
    return HPSocket.HandleResult.Ok;
}


HPSocket.HandleResult TcpServer_OnReceive(HPSocket.IServer sender, IntPtr connId, byte[] data)
{

   var dst= ModbusDevicesHelp.BytesToString(data);
    Console.WriteLine(dst);
    return HPSocket.HandleResult.Ok;
}

tcpServer.Start();
while (true)
{
    var key = Console.ReadLine();
    if (key == "1")
    {
        foreach (var item in intPtrs)
        {
            var data = new Random().Next(0, 11111111);
            var sdata = BitConverter.GetBytes(data);
            tcpServer.Send(item, sdata, sdata.Length);
        }
    }
    if (key == "9")
    {
        //IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), sendport);
        //sendUDP.Connect(endpoint);
        //endpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), reveiveport);
        //receiveUDP.Connect(endpoint);
        IPEndPoint endpoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), reveiveport);
        receiveUDP = new System.Net.Sockets.UdpClient(endpoint);
    }
    if (key == "10")
    {
        Task.Factory.StartNew(() =>
        {
            while (true)
            {
                //using (UdpClient udp = new UdpClient())
                //{
                //    IPEndPoint sendPort = new IPEndPoint(IPAddress.Parse("127.0.0.1"), reveiveport);
                //    var data = Encoding.UTF8.GetBytes($"发送端口:{reveiveport}-{name}{DateTime.Now.ToString()}");
                //    //udpClient.BeginSend(data, data.Length, remoteEndPoint, null, null);
                //    udp.Send(data, data.Length, sendPort);
                //}

                if (receiveUDP.Available > 0)
                {
                    EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
                    //IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), reveiveport);
                    // 设置一个64k的字节数组作为缓存
                    byte[] data = new byte[65536];
                    var dst = receiveUDP.Client.ReceiveFrom(data,ref remoteEndPoint);
                    string receivedData = Encoding.UTF8.GetString(data);
                    Console.WriteLine($"{name}-接收端口:{((System.Net.IPEndPoint)remoteEndPoint).Port}+ {receivedData}");
                }

                Thread.Sleep(1000);
            }
        });
    }
}